package com.bytenebula.data.jdbc.mysql;

import cn.hutool.json.JSONObject;
import com.bytenebula.data.JdbcUrlBuilder;
import com.bytenebula.entity.meta.MetaDatabase;

import java.util.Map;

/**
 * 生成MYSQL Jdbc URL字符串
 */
public class MysqlJdbcUrlBuilder extends JdbcUrlBuilder {
    /**
     * 处理JDBC URL扩展参数
     * @param url JDBC URL
     * @param database 数据库对象
     * @return 返回Jdbc URL参数字符串
     */
    @Override
    protected String buildParams(String url, MetaDatabase database) {
        if (database.getJdbcParams() != null) {
            JSONObject jsonobj = new JSONObject(database.getJdbcParams());
            StringBuilder paramsBuilder = new StringBuilder();
            for (Map.Entry<String, Object> entry : jsonobj.entrySet()) {
                paramsBuilder.append(String.format("&%s=%s", entry.getKey(), entry.getValue().toString()));
            }
            if (!paramsBuilder.isEmpty()) {
                url = url + "?" + paramsBuilder;
            }
        }
        return url;
    }
}
